package pannelli;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.DocumentException;

import database.DB;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.*;


import oggetti.Cliente;
import oggetti.PDF;
import oggetti.Riparazione;
import pannelli.main.PannelloClienti;
import pannelli.sub.SubPannelloRiparazione;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class AggiungiRiparazione extends javax.swing.JFrame {

    private SubPannelloRiparazione subPannelloRiparazione1;
    private JButton aggiungi;
    private Cliente c;
    private int where;

    public AggiungiRiparazione(Cliente c,int where) {
        super();
        this.c = c;
        this.where=where;
        initGUI();
        centra();
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
     
    private void initGUI() {
        try {
            AnchorLayout thisLayout = new AnchorLayout();
            getContentPane().setLayout(thisLayout);
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            {
                aggiungi = new JButton();
                getContentPane().add(aggiungi, new AnchorConstraint(782, 709, 893, 326, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
                aggiungi.setText("Aggiungi riparazione");
                aggiungi.setPreferredSize(new java.awt.Dimension(236, 31));
                aggiungi.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent arg0) {
                        addRiparazione();
                    }
                });
            }
            {
                subPannelloRiparazione1 = new SubPannelloRiparazione();
                getContentPane().add(subPannelloRiparazione1, new AnchorConstraint(1, 1001, 670, 1, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
                subPannelloRiparazione1.setPreferredSize(new java.awt.Dimension(400, 186));
                subPannelloRiparazione1.riempiIdCliente(c.getId());
                subPannelloRiparazione1.riempiID();
            }
            pack();
            this.setSize(615, 300);
            setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void addRiparazione() {
        Riparazione r = subPannelloRiparazione1.getRiparazioneFromFields();
        if (r != null) {
            if (new DB().addRiparazione(r)) {
                try {
                    JOptionPane.showMessageDialog(null, "Riparazione aggiunta con successo!");
                    dispose();
                    if(where==0){
                        PannelloClienti.getIstance().updateTable();
                        PannelloClienti.getIstance().activeButtons();
                        PannelloClienti.getIstance().setEnabled(true);
                    }else if(where==1){
                        VisualizzaCliente.getInstance().updateTable();
                    }
                    PDF.writePDFAccettazione(c, r);
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(AggiungiRiparazione.class.getName()).log(Level.SEVERE, null, ex);
                } catch (DocumentException ex) {
                    Logger.getLogger(AggiungiRiparazione.class.getName()).log(Level.SEVERE, null, ex);
                } catch (MalformedURLException ex) {
                    Logger.getLogger(AggiungiRiparazione.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(AggiungiRiparazione.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {
                JOptionPane.showMessageDialog(null, "Si e' verificato un errore col database!");
            }
        }
    }

    public void centra() {
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation(new Point((dimension.width - getSize().width) / 2,
                (dimension.height - getSize().height) / 2));

    }
}
